<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>
    <base href="<%=basePath%>">
    <title>业务角色用户列表</title>
    <script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
    <script src="<%=basePath%>component/grid.js"></script>
    <script type="text/javascript" src="<%=basePath%>dwrsystem/interface/userService.js"></script> 
	<script type="text/javascript" src="<%=basePath%>dwrsystem/engine.js"></script>
	<script src="<%=basePath%>dwrsystem/engine.js"></script>
	<script src="<%=basePath%>dwrsystem/interface/roleBusinessService.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/engine.js"></script>
	<script type="text/javascript" src="<%=basePath%>dwrsystem/interface/UserRolebusinessAuthorize.js"></script>
	<script type="text/javascript">
	var mygrid;
	var userWindow;
	var roleBusinessId;
	var dhxLayout = createLayoutNormal('1C',false,true);
	
	$(function (){
		roleBusinessId = '${param.roleBusinessId}';
		configLayout();
	 	dhxGrid.initDataTable();
	});

	function configLayout(){
		var a = dhxLayout.cells('a');
		a.hideHeader();
		a.setText('主页面');
		a.fixSize(true, true);
		mygrid = createGridFromLayoutCell(a);
		if(power.getPower() != '' && (power.getPowerInsert() || power.getPowerDelete()))
			createToolbarFromDHXLayoutCell(a,function(toolbar){
			    toolbar.addText("info", 1, "用户列表:");
			    toolbar.addSeparator("sep1", 2);
			    if(power.getPowerInsert())
			    	toolbar.addButton("add", 3, "管理用户", "new.gif", "new_dis.gif");
			    if(power.getPowerDelete())
			    	toolbar.addButton("delete", 4, "删除用户", "cut.gif", "cut_dis.gif");
			    if(power.getPowerUpdate()){
			    	toolbar.addButton("updateUp", 5, "上移用户", "arrow_up.png", "arrow_up.png");
			    	toolbar.addButton("updateDown", 6, "下移用户", "arrow_down.png", "arrow_down.png");
			    }
			    toolbar.attachEvent("onClick", function(id) {
			    	if(id == 'add'){
			    		queryUsers();
			    	}
			    	if(id == 'delete'){
			    		dhxGrid.deleteUser();
			    	}
			    	if(id == 'updateUp'){
			    		dhxGrid.rowUp();
			    	}
			    	if(id == 'updateDown'){
			    		dhxGrid.rowDown();
			    	}
			    });
			});
	}
	
	var dhxGrid = {
		initDataTable : function () {
		    mygrid.setHeader("序号,登录姓名,登录账号,备注");
		    mygrid.setInitWidths("50,200,200,*");
		    mygrid.setColAlign("right,left,left,left");
		    mygrid.setColSorting("int,str,str,str");
		    mygrid.setColTypes("ro,ro,ro,ro");
		    mygrid.init();
		    setSkin(mygrid);
		    mygrid.loadXML('<%=basePath%>system-user/UserAction!userGridFromRoleBusiness.action?userForm.roleBusinessId=' + roleBusinessId);
		},
		deleteUser : function (){
			if(mygrid.getSelectedId()){
				confirmInfo('确认删除？',
					function(choose){
						if('ok' == choose){
							layoutProgressOn(dhxLayout);
							userService.deleteTUserRoleBusiness(mygrid.getSelectedId(),roleBusinessId,function(data){
								var lastrowIndex = mygrid.getRowIndex(mygrid.getSelectedId())-1;
								mygrid.deleteSelectedRows();
								mygrid.selectRow(lastrowIndex);
								
								//修改前面的序号
								for(var i=(lastrowIndex+1) ;i<(mygrid.getRowsNum());i++){
									mygrid.cells(mygrid.getRowId(''+i),0).setValue(i+1);
								}
								parent.parent.notifyCreater('删除成功');
								layoutProgressOff(dhxLayout);
							});
						}else{
							layoutProgressOff(dhxLayout);
							return;
						}
					}
				);
			} else parent.parent.notifyWarn('请选择删除项');
		},
		rowUp : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId) {
				this.sort('up',rowId);
			}else{
				parent.parent.notifyWarn('请选择移动项');
			}
		},
		rowDown : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId){
				this.sort('down',rowId);
			}else{
				parent.parent.notifyWarn('请选择移动项');
			}
		},
		sort : function (status, id){
	        var rInd = mygrid.getRowId((status=='up'?(mygrid.getRowIndex(id)-1):(mygrid.getRowIndex(id)+1)));
	        if(!rInd)	return ;
	        layoutProgressOn(dhxLayout);
	        userService.selectTUserSortNumFromRoleBusiness(roleBusinessId, rInd, function(data){
				if(data != null){
					userService.sortUserInRoleBusiness(data, id, roleBusinessId);
					if(status == 'up'){
						mygrid.moveRowUp(id);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)),0).setValue(mygrid.getRowIndex(id)+1);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)+1),0).setValue(mygrid.getRowIndex(id)+2);
					}
					else{
						mygrid.moveRowDown(id);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)-1),0).setValue(mygrid.getRowIndex(id));
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)),0).setValue(mygrid.getRowIndex(id)+1);
					}
					parent.parent.notifyCreater('排序成功');
					layoutProgressOff(dhxLayout);
				}else{
					parent.parent.notifyWarn('排序发生错误');
					layoutProgressOff(dhxLayout);
					return;
				}
			});
		}
	};
	
	function queryUsers(){
		roleBusinessService.selectExitsUsers({roleBusinessId:roleBusinessId},function(data){
			userWindow = openNewWindowURL('选择用户','<%=basePath%>page/system/chooseComponent/chooseUser.jsp?userIds='+data);
		});
	}

	function closeUsers(){
		if(userWindow){
			userWindow.close();
		}
	}
	
	function getChooseUsesrResult(ids,names,loginCodes,newaddIds,newaddNames,newaddLoginCodes,deletedIds){
		layoutProgressOn(dhxLayout);
		if((newaddIds != null && newaddIds != '') || (deletedIds != null && deletedIds != '')){
			UserRolebusinessAuthorize.deleteAndinsertAllUsersInOneRoleBusiness(newaddIds,deletedIds,roleBusinessId,function(data){
				if(data){
					updateGrid(newaddIds,newaddNames,newaddLoginCodes,deletedIds);
					layoutProgressOff(dhxLayout);
					closeUsers();
				}else{
					parent.parent.notifyWarn('系统异常，请联系管理员');
					layoutProgressOff(dhxLayout);
				}
			});
		}else{
			parent.parent.notifyCreater('没有操作');
			layoutProgressOff(dhxLayout);
			closeUsers();
		}
	}

	function updateGrid(newaddIds,newaddNames,newaddLoginCodes,deletedIds){
		if(deletedIds != null && deletedIds != ''){
			var delIds = deletedIds.split(',');
			for(var j=0;j<delIds.length;j++){
				mygrid.deleteRow(delIds[j]);
				for(var i=0 ;i<(mygrid.getRowsNum());i++){
					mygrid.cells(mygrid.getRowId(''+i),0).setValue(i+1);
				}
			}
		}
		if(newaddIds != null && newaddIds != ''){
			var addIds = newaddIds.split(',');
			var names = newaddNames.split(',');
			var codes = newaddLoginCodes.split(',');
			for(var i=0;i<addIds.length;i++){
				mygrid.addRow(addIds[i], ''+(mygrid.getRowsNum()+1), ''+mygrid.getRowsNum());
				mygrid.cellById(addIds[i], 1).setValue(names[i]);
				mygrid.cellById(addIds[i], 2).setValue(codes[i]);
				if(i == (addIds.length-1)){
					mygrid.selectRowById(addIds[i]);
				}
			}
		}
		parent.parent.notifyCreater('操作成功');
	}
	</script>
</head>

<body>
</body>

</html>
